package controllers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import controllers.CRUD.ObjectType;

import play.db.Model;
import play.exceptions.TemplateNotFoundException;
import play.mvc.With;
import models.Friends;
import models.LendCard;
import models.MemType;
import models.Member;
import models.MyBookMsg;
import models.SpaceLog;
import models.UserCar;


@With(Secure.class)
@CRUD.For(Member.class)
public class Members extends CRUD {
	
	 @Check("UserControl")
	 public static void list(int page, String search, String searchFields, String orderBy, String order) {
	        ObjectType type = ObjectType.get(getControllerClass());
	        notFoundIfNull(type);
	        if (page < 1) {
	            page = 1;
	        }
	        List<Model> objects = type.findPage(page, search, searchFields, orderBy, order, (String) request.args.get("where"));
	        Long count = type.count(search, searchFields, (String) request.args.get("where"));
	        Long totalCount = type.count(null, null, (String) request.args.get("where"));
	        try {
	            render(type, objects, count, totalCount, page, orderBy, order);
	        } catch (TemplateNotFoundException e) {
	            render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order);
	        }
	    }
	
	public static  void mybook(String uid){
		Member meb=Member.findById(Long.parseLong(uid));
		if(meb == null){
			render("proscenium/login.html");
		}
		MemType memtype = MemType.findById(Long.parseLong(meb.metid));
		LendCard lendcard = LendCard.find("from LendCard where lecnum = ? ", meb.lecnum).first();
		
		List<MyBookMsg> mbMsglist = MyBookMsg.find("from MyBookMsg where memid = ? order by msgtime desc ",Long.parseLong(uid)).fetch();
		List<UserCar> carlist = UserCar.find("userid=? and type=1",uid).fetch();
		List<SpaceLog> spaceloglist = SpaceLog.find("from SpaceLog where memid = ?",meb.id).fetch();
		System.out.println(spaceloglist+"-------------------------ddddddddddddd");
		List<Friends> flist = Friends.find("from Friends where pid = ?", meb.id).fetch() ;
		render("/proscenium/study.html",meb,memtype,lendcard,mbMsglist,uid,carlist,spaceloglist,flist);
	}
	
	public static void savemsg(String uid,String content){
		int state = 0 ;
		try{
			 String userid = session.get("userid");
			 Member meb=Member.findById(Long.parseLong(userid));
			 MyBookMsg mybookmsg =new MyBookMsg();
			 mybookmsg.memid = Long.parseLong(uid);
			 mybookmsg.tomemid = meb.id;
			 mybookmsg.content = content ;
			 SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy-MM-dd HH:mm:ss ");   
			 Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间   
			 System.out.println(curDate);
			 String   starttime   =   formatter.format(curDate); 
			 System.out.println(starttime+"---------");
			 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
			 java.util.Date joindateDate = sdf.parse(starttime);
			 mybookmsg.msgtime = joindateDate ;
			 mybookmsg.save();
			 state = 1 ;
			 render(state);
		}catch(Exception e){
			state = 0 ;
			e.printStackTrace();
			render(state);
		}
	}
	
	public static void forlog(){
		String uid = session.get("userid");
		render("/proscenium/addlog.html",uid);
	}
	
	public static void  forallLog(Long logid){
		SpaceLog spacelog = SpaceLog.findById(logid);
		render("/proscenium/allLog.html",spacelog);
	}
	
	public static void addlog(String logtitle,String logcontent){
		int state = 0 ;
		String uid = session.get("userid");
		try{
			SpaceLog spacelog = new SpaceLog();
			spacelog.spatit = logtitle ;
			spacelog.spadet = logcontent ;
			spacelog.memid = Long.parseLong(uid);
			
			SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy-MM-dd HH:mm:ss ");   
			 Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间   
			 System.out.println(curDate);
			 String   starttime   =   formatter.format(curDate); 
			 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
			 java.util.Date joindateDate = sdf.parse(starttime);
			 
			 spacelog.spatime = joindateDate ;
			 spacelog.save();
			 state = 1 ;
			 render(state);
		}catch(Exception e){
			state = 0 ;
			e.printStackTrace();
			render(state);
		}
		
	}
	
	public static void dellog(Long logid){
		int state = 0 ;
		System.out.println(logid+"|||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
		try{
			int  dd = SpaceLog.delete("from SpaceLog where id = ? ", logid);
			System.out.println("|||||||||||||||||||||||||||||||||||||||||||||||||||||||||"+dd);
			if(dd > 0){
				state = 1 ;
				render(state);
			}else{
				state = 0 ;
				render(state);
			}
		}catch(Exception e){
			state = 0 ;
			e.printStackTrace();
			render(state);
		}
	}
	
	public static void addfd(Long mid){
		int state = 0  ;
		String uid = session.get("userid");
		try{
			Friends friends = new Friends();
			friends.fid = mid ;
			friends.pid = Long.parseLong(uid);
			friends.save() ;
			state = 1 ;
			render(state);
		}catch(Exception e){
			state = 0 ;
			e.printStackTrace() ;
			render(state);
		}
	}
}
